package cn.qingyun.gis.modules.scenes.service.impl;

import cn.qingyun.gis.modules.express.entity.Point;
import cn.qingyun.gis.modules.express.util.GeoTool;
import cn.qingyun.gis.modules.express.util.LocationTool;
import cn.qingyun.gis.modules.express.util.SyncTool;
import cn.qingyun.gis.modules.scenes.entity.RegionAddr;
import cn.qingyun.gis.modules.scenes.mapper.RegionAddrMapper;
import cn.qingyun.gis.modules.scenes.service.IRegionAddrService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author LHJ
 * @description
 * @date 2024/9/5 17:27
 */
@Slf4j
@Service
@DS("scenes_offensive")
public class IRegionAddrServiceImpl extends ServiceImpl<RegionAddrMapper, RegionAddr> implements IRegionAddrService {

    @Resource
    private IRegionAddrService regionAddrService;

    @Override
    @Transactional
    public void wkt84To02AndCalcPoint() {
        log.debug("region_addr表进入计算中心点方法");
        int batchSize = 500;
        int pages = (this.count() / batchSize) + 1;
        //并发导入
        SyncTool.doSync(pages, i -> {
            Page<RegionAddr> page = new Page<>(i, 500);
            List<RegionAddr> records = this.baseMapper.selectPage(page, null).getRecords();
            // 将边界类型进行 84 转 02
            for (RegionAddr regionAddr : records) {
                String geom = LocationTool.convertWkts(regionAddr.getGeometry());
                regionAddr.setGeometry(geom);
                Point centerPoint = GeoTool.getCenterPoint(geom);
                if (centerPoint!=null){
                    if (centerPoint.getLat()!=null){
                        regionAddr.setLat(centerPoint.getLat());
                    }
                    if (centerPoint.getLon()!=null){
                        regionAddr.setLon(centerPoint.getLon());
                    }
                }
                regionAddr.setUpdateTime(new Date());
            }
            //用this.updateBatchById老不好使，烦人
            regionAddrService.updateBatchById(records);
            log.debug("更新成功！");
        });
        log.debug("执行完毕！！！");
    }
}
